<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>文档</title>
  <link href="../css/semantic/semantic.min.css" rel="stylesheet">
  <link href="../css/markdown.css" rel="stylesheet">
    <link href="../css/wtxynavigation.css" rel="stylesheet">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/include.js"></script>
  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">
    <div id="project-title">
      <div class="title-wrap">
        <div class="left">
          <i class="icon eye"></i>
          文档预览:
          mianmSDK
        </div>
        <div class="right">
          Export by Gitee
        </div>
      </div>
    </div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>15怎样利用GCD实现多度单写</div><div class='content'><h1>
<a id="怎样利用gcd实现多读单写" class="anchor" href="#%E6%80%8E%E6%A0%B7%E5%88%A9%E7%94%A8gcd%E5%AE%9E%E7%8E%B0%E5%A4%9A%E8%AF%BB%E5%8D%95%E5%86%99"></a>怎样利用GCD实现多读单写?</h1>
<p><code>dispatch_barrier_async(&lt;#dispatch_queue_t _Nonnull queue#&gt;, &lt;#^(void)block#&gt;)</code></p>
<h2>
<a id="描述" class="anchor" href="#%E6%8F%8F%E8%BF%B0"></a>描述</h2>
<ol>
<li>读者和读者是并发的</li>
<li>读者和写者是互斥的</li>
<li>有读的线程在读数据的时候, 就不能有写的线程去写数据</li>
<li>写者和写者要互斥</li>
</ol>
<h3>
<a id="1" class="anchor" href="#1"></a>1.</h3>
<h1>
<a id="答--使用dispatch_barrier_async" class="anchor" href="#%E7%AD%94--%E4%BD%BF%E7%94%A8dispatch_barrier_async"></a>答:  使用<code>dispatch_barrier_async</code>
</h1>
<ol>
<li>在读端使用 同步+并发队列</li>
<li>在写端使用 <code>dispatch_barrier_async</code> + 并发队列</li>
</ol>
<pre><code>
#import "WTUserCenter.h"

@interface WTUserCenter ()

{
    
    // 定义一个并发队列
    dispatch_queue_t concurrent_queue;
    // 用户数据中心, 可能有多个线程需要数据访问
    NSMutableDictionary *userCenterDic;
}

@end

@implementation WTUserCenter

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        
        concurrent_queue = dispatch_queue_create("com.itopengl", DISPATCH_QUEUE_CONCURRENT);
        userCenterDic = [NSMutableDictionary dictionary];
    }
    
    return self;
}


- (id)wtObjectForKey:(NSString *)key
{
    __block id obj;
    
    // 同步读取指定数据
    dispatch_sync(concurrent_queue, ^{
       
        obj = [userCenterDic objectForKey:key];
    });
    
    return obj;
}


- (void)wtSetObject:(id)obj forKey:(NSString *)key
{
    // 异步栅栏调用设置数据
    
    dispatch_barrier_async(concurrent_queue, ^{
        
        [self-&gt;userCenterDic setObject:obj forKey:key];
    });

}

@end


</code></pre></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>